﻿using System.Runtime.InteropServices;
using System.Xml;

namespace XmlSockets.Internal
{
    /// <summary>
    /// 
    /// </summary>
    internal sealed class XmlMessageWriter
    {
        private XmlMessageWriteBufferdStream stream;

        public XmlWriter InternalWriter
        {
            get;
            private set;
        }


        public IXmlSocketSendContent CreateContent(CreateXmlSocketSendContentHandler handler)
        {
            return stream.CreateContent( handler);
        }

        public void Clear()
        {
            stream.Clear();
        }



        /// <summary>
        /// 
        /// </summary>
        private readonly static XmlWriterSettings writerSettings = CreateWriterSettings();



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static XmlWriterSettings CreateWriterSettings()
        {
            XmlWriterSettings writerSettings = new XmlWriterSettings();
            writerSettings.Encoding = XmlSocketConstant.XmlSocketEncoding;
            writerSettings.ConformanceLevel = ConformanceLevel.Fragment;
#if !NET40
            writerSettings.Async = true;
#endif
            
            return writerSettings;

        }


        internal XmlMessageWriter(int size)
        {
            InternalWriter = CreateWriter(size);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>

        private XmlWriter CreateWriter(int size)
        {
            stream = new XmlMessageWriteBufferdStream(size);
            return XmlWriter.Create(stream, writerSettings);
        }








    }
}
